home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Magazine / C_Tutorial / Part-10 / frac0 / fractal.c < prev    next >
C/C++ Source or Header  |  1998-03-02  |  1KB  |  47 lines

  1. #include "fractal.h"
  2.  
  3. #include "gadgets.h"
  4.  
  5. #include<clib/graphics_protos.h>
  6.  
  7. static int calc(float, float, int);
  8.  
  9. /* Draw a fractal in the window */
  10. void drawFractal(struct Window* win)
  11. {    
  12.     /* The width, height and number of colours for drawing */
  13.     int w=win->Width, h=win->Height, d=1<<win->WScreen->BitMap.Depth;
  14.     /* The snapshot of the mandelbrot set to draw */
  15.     /* (Adjust these numbers to draw different fractals) */
  16.     float width=4.0, height=4.0, top=-2.0, left=-2.5;
  17.     int x;
  18.     for(x=0; x<w; x++)
  19.     {
  20.         int y;
  21.         for(y=0; y<h; y++)
  22.         {
  23.             /* Set the current colour */
  24.             setFgPen(win,    calc(x*width/w+left,y*height/h+top,d));
  25.             /* Draw the pixel */
  26.             WritePixel(win->RPort,x,y);
  27.         }
  28.     }
  29. }
  30.  
  31. /* Calculate the colour of a particular point */
  32. /* (This is the number of iterations of the equation */
  33. /* needed to exceed the bound value) */
  34. static int calc(float x, float y, int d)
  35. {
  36.     float xc=x, yc=y;
  37.     int it;
  38.     /* Adjust the 16.0 to give different colour spreads */
  39.     for(it=0; it<d && (xc*xc)+(yc*yc)<16.0; it++)
  40.     {
  41.         float oldx = xc;
  42.         xc = oldx*oldx-yc*yc+x;
  43.         yc = 2.0*oldx*yc+y;
  44.     }
  45.     return it;
  46. }
  47.